package com.yx.yinshanjinniu.adapters;

import android.content.Context;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.activitys.HotTopicActivity;
import com.yx.yinshanjinniu.application.MyApplication;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.base.BaseRVAdapter;
import com.yx.yinshanjinniu.bean.CalendarBean;
import com.yx.yinshanjinniu.bean.DayBean;
import com.yx.yinshanjinniu.bean.FollowOrderBean;
import com.yx.yinshanjinniu.bean.GifBean;
import com.yx.yinshanjinniu.bean.HomeHotBean;
import com.yx.yinshanjinniu.bean.ImageItemBean;
import com.yx.yinshanjinniu.bean.ImportantBean;
import com.yx.yinshanjinniu.bean.NewsNoticeBean;
import com.yx.yinshanjinniu.bean.SellOrderInfoBean;
import com.yx.yinshanjinniu.bean.ShopGoodsListBean;
import com.yx.yinshanjinniu.core.AppAction;
import com.yx.yinshanjinniu.interfaces.OnItemClickListener;
import com.yx.yinshanjinniu.utils.DataUtils;
import com.yx.yinshanjinniu.utils.FastClickUtil;
import com.yx.yinshanjinniu.utils.GlideUtils;
import com.yx.yinshanjinniu.utils.LogUtil;
import com.yx.yinshanjinniu.utils.RouteUtil;
import com.yx.yinshanjinniu.utils.ToastUtil;
import com.yx.yinshanjinniu.view.ImageCycleView;
import com.yx.yinshanjinniu.view.MarqueeTextView;
import com.yx.yinshanjinniu.view.MyGridView;
import com.yx.yinshanjinniu.view.TextViewSwitcher;
import com.zc.mychart.model.GoodsAllBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private LayoutInflater layoutInflater;
    private AppAction mAppAction;
    private HomeListener mHomeListener;
    private List<NewsNoticeBean> mNewsNoticeBeanList = new ArrayList<>();//公告
    private List<ImageItemBean> mImageList = new ArrayList<>();//banner
    private List<GoodsAllBean> mGoodsList = new ArrayList<>();//热门商品
    private Map<String, GifBean> mGifMap = new HashMap<>();//广告位
    private ImportantBean impDataBean;//重要数据
    private List<HomeHotBean> mHomeHotList = new ArrayList<>();//热点资讯
    private List<SellOrderInfoBean> profitSquareList = new ArrayList<>();//牛单
    private boolean isShowTip;//是否显示安全提示
    private List<ShopGoodsListBean> mHomeGoodsList = new ArrayList<>();
    private List<FollowOrderBean> mFollowList = new ArrayList<>();
    private List<CalendarBean> mCalendarList = new ArrayList<>();
    private List<DayBean> mDayList = new ArrayList<>();

    private boolean isResetTimer = false;
    private int mChooseProfit = 0;//0牛单 1日历 2商品
    //    private FollowOrderRequestBean mFollowOrderRequestBean;
    private MyApplication myApplication;
    private BaseActivity mActivity;

    private final int HEADER_INDEX = 0;//顶部header
    private final int BANNER_INDEX = 1;//banner
    private final int MENU_INDEX = 2;//menu图标
    private final int PROBEAN_INDEX = 3;//热门商品
    private final int ADVERT_INDEX = 4;//广告位
    private final int IMPORTANT_INDEX = 5;//重要数据
    private final int PROFIT_SQUARE = 6;//牛单广场
    private final int TIP_INDEX = 7;//安全提示
    private final int FRONT_FIXED_ITEM_COUNT = 8;//商城和跟单中心
    private HotProViewHolder hotProViewHolder;


    public HomeAdapter(Context context, HomeListener listener) {
        mContext = context;
        mActivity = (BaseActivity) context;
        layoutInflater = LayoutInflater.from(mContext);
        mHomeListener = listener;
        myApplication = ((MyApplication) mContext.getApplicationContext());
        mAppAction = ((MyApplication) mContext.getApplicationContext()).getAppAction();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HEADER_INDEX:
                return new HeadViewHolder(layoutInflater.inflate(R.layout.item_type_home_head, parent, false));
            case BANNER_INDEX:
                return new BannerViewHolder(layoutInflater.inflate(R.layout.item_type_home_banner, parent, false));
            case MENU_INDEX:
                return new MenuViewHolder(layoutInflater.inflate(R.layout.item_type_home_menu, parent, false));
            case PROBEAN_INDEX:
                hotProViewHolder = new HotProViewHolder(layoutInflater.inflate(R.layout.item_type_home_hot_pro, parent, false));
                return hotProViewHolder;
            case ADVERT_INDEX:
                return new AdvertViewHolder(layoutInflater.inflate(R.layout.item_type_home_advert, parent, false));
            case IMPORTANT_INDEX:
                return new ImportantDataViewHolder(layoutInflater.inflate(R.layout.item_type_home_important_data, parent, false));
            case PROFIT_SQUARE:
                return new ProfitSquareViewHolder(layoutInflater.inflate(R.layout.item_type_home_square_list, parent, false));
            case TIP_INDEX:
                return new SafeTipViewHolder(layoutInflater.inflate(R.layout.item_type_home_safe_tip, parent, false));
            case FRONT_FIXED_ITEM_COUNT:
                return new MarketAndOrderViewHolder(layoutInflater.inflate(R.layout.item_type_home_market_order, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        long startTime = System.currentTimeMillis();
        if (holder instanceof HeadViewHolder) {
            ((HeadViewHolder) holder).setData();
        } else if (holder instanceof BannerViewHolder) {
            ((BannerViewHolder) holder).setData();
        } else if (holder instanceof MenuViewHolder) {
            ((MenuViewHolder) holder).setData();
        } else if (holder instanceof HotProViewHolder) {
            ((HotProViewHolder) holder).setData();
        } else if (holder instanceof AdvertViewHolder) {
            ((AdvertViewHolder) holder).setData();
        } else if (holder instanceof ImportantDataViewHolder) {
            ((ImportantDataViewHolder) holder).setData();
        } else if (holder instanceof ProfitSquareViewHolder) {
            ((ProfitSquareViewHolder) holder).setData();
        } else if (holder instanceof SafeTipViewHolder) {
            ((SafeTipViewHolder) holder).setData();
        } else if (holder instanceof MarketAndOrderViewHolder) {
            ((MarketAndOrderViewHolder) holder).setData();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position < FRONT_FIXED_ITEM_COUNT) {
            return position;
        } else {
            return FRONT_FIXED_ITEM_COUNT;
        }
    }

    @Override
    public int getItemCount() {
        return FRONT_FIXED_ITEM_COUNT + 1;
    }

    public void setNewsNoticeList(List<NewsNoticeBean> bean) {
        mNewsNoticeBeanList = bean;
        notifyItemChanged(HEADER_INDEX, true);
    }

    public void updateNewer() {
        notifyItemChanged(HEADER_INDEX, true);
    }

    public void setImageList(List<ImageItemBean> imageList) {
        mImageList = imageList;
        notifyItemChanged(BANNER_INDEX, true);
    }

    public void setDayList(List<DayBean> dayList) {
        mDayList = dayList;
        notifyItemChanged(FRONT_FIXED_ITEM_COUNT, true);
    }

    public void setGoodsList(List<GoodsAllBean> goodsList) {
        this.mGoodsList = goodsList;
        notifyItemChanged(PROBEAN_INDEX, true);
//        if (hotProViewHolder != null){
//            hotProViewHolder.setData();
//        }
    }

    public void setGifList(Map<String, GifBean> gifMap) {
        this.mGifMap = gifMap;
        notifyItemChanged(ADVERT_INDEX, true);
    }

    public void setImportantData(ImportantBean bean) {
        long endTime = 0;
        if (impDataBean != null) {
            endTime = impDataBean.end_time;
        }
        isResetTimer = impDataBean == null || Math.abs(bean.end_time - endTime) > 10;//初始化或修正时间误差
        impDataBean = bean;
        if (!isResetTimer) {
            impDataBean.end_time = endTime;//保留原时间
        }
        notifyItemChanged(IMPORTANT_INDEX, true);
    }

    public void setProfitSquareList(List<SellOrderInfoBean> list) {
        profitSquareList = list;
        notifyItemChanged(PROFIT_SQUARE, true);
    }

    public void setHomeHotList(List<HomeHotBean> hotList) {
        this.mHomeHotList = hotList;
        notifyItemChanged(IMPORTANT_INDEX, true);
    }

    public void setShowTip(boolean showTip) {
        isShowTip = showTip;
        notifyItemChanged(TIP_INDEX, true);
    }

    public void setHomeGoodsList(List<ShopGoodsListBean> list) {
        this.mHomeGoodsList = list;
//        notifyBottomTypeList();
    }

    public void setFollowList(List<FollowOrderBean> followList) {
        this.mFollowList = followList;
        notifyBottomTypeList();
    }

//    public void setDayList(List<DayBean> dayList) {
//        mDayList = dayList;
//        notifyItemChanged(FRONT_FIXED_ITEM_COUNT, true);
//    }

    public void setEconomicsList(List<CalendarBean> dayList) {
        mCalendarList = dayList;
        notifyItemChanged(FRONT_FIXED_ITEM_COUNT, true);
    }

    public void notifyBottomTypeList() {
        notifyItemChanged(FRONT_FIXED_ITEM_COUNT, true);
    }

    public boolean isNoticeEmpty() {
        return mNewsNoticeBeanList.isEmpty();
    }

    public boolean isBannerEmpty() {
        return mImageList.isEmpty();
    }

    public boolean isHotEmpty() {
        return mGoodsList.isEmpty();
    }

    public boolean isGifEmpty() {
        return mGifMap.isEmpty();
    }

    public boolean isImportantEmpty() {
        return impDataBean == null;
    }

    public boolean isHomeHotEmpty() {
        return mHomeHotList.isEmpty();
    }

    public boolean isProfitSquareEmpty() {
        return profitSquareList.isEmpty();
    }

    public boolean isHomeGoodsEmpty() {
        return mHomeGoodsList.isEmpty();
    }

    public boolean isEconomicsEmpty() {
        return mCalendarList.isEmpty();
    }

    public boolean isFollowEmpty() {
        return mFollowList.isEmpty();
    }

    public boolean isDayListEmpty() {
        return mDayList.isEmpty();
    }

    public List<FollowOrderBean> getmFollowList() {
        return mFollowList;
    }

    private class HeadViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, ImageCycleView.ImageCycleViewListener {
        private LinearLayout ll_customer_service;
        private TextView tv_app_name;
        private View ll_message;
        private View view_message_dot;
        private RelativeLayout rl_notice;
        private TextViewSwitcher ts_notice;
        private ImageView iv_guide;


        HeadViewHolder(View itemView) {
            super(itemView);

            ll_customer_service = itemView.findViewById(R.id.ll_customer_service);
            ll_message = itemView.findViewById(R.id.ll_message);
            tv_app_name = itemView.findViewById(R.id.tv_app_name);
            view_message_dot = itemView.findViewById(R.id.view_message_dot);
            iv_guide = itemView.findViewById(R.id.iv_guide);
            rl_notice = itemView.findViewById(R.id.rl_notice);
            ts_notice = itemView.findViewById(R.id.ts_notice);

            ll_customer_service.setOnClickListener(this);
            ll_message.setOnClickListener(this);
            tv_app_name.setOnClickListener(this);
            iv_guide.setOnClickListener(this);
        }

        private void setData() {
            LogUtil.d("head setdata");
            boolean hasMessage = DataUtils.getBooleanSPByTag(mContext, DataUtils.SP_HAS_MESSAGE);
            if (hasMessage) {
                view_message_dot.setVisibility(View.VISIBLE);
            } else {
                view_message_dot.setVisibility(View.GONE);
            }

            if (!DataUtils.isLogin(mContext)) {
                iv_guide.setVisibility(View.VISIBLE);
            } else {
                iv_guide.setVisibility(View.GONE);
            }

            if (null != mNewsNoticeBeanList && mNewsNoticeBeanList.size() > 0) {
//                rl_notice.setVisibility(View.VISIBLE);
                ts_notice.setAdapter(new RollingTextAdapter() {
                    @Override
                    public int getCount() {
                        return mNewsNoticeBeanList.size() > 0 ? mNewsNoticeBeanList.size() : 0;
                    }

                    @Override
                    public View getView(Context context, View contentView, int position) {
                        View view = View.inflate(context, R.layout.item_notice_scroll, null);
                        MarqueeTextView tv_report = view.findViewById(R.id.it_tv);
                        String desc = mNewsNoticeBeanList.get(position).title;
                        tv_report.setText(desc);
                        return view;
                    }
                });
                ts_notice.setOnItemClickListener(new TextViewSwitcher.OnItemClickListener() {
                    @Override
                    public void onClick(int position) {
                        RouteUtil.toWebViewActivity(mActivity, mNewsNoticeBeanList.get(position).url);
                    }
                });

                if (mNewsNoticeBeanList.size() > 1) {
                    ts_notice.startFlipping();
                }
            } else {
                rl_notice.setVisibility(View.GONE);
            }
        }

        @Override
        public void onClick(View v) {
            if (FastClickUtil.isFastClick()) {
                return;
            }
            switch (v.getId()) {
                case R.id.ll_customer_service:
                    mHomeListener.onClickCustomService();
                    break;
                case R.id.ll_message:
                    mHomeListener.onClickMessage();
                    break;
                case R.id.tv_app_name:
                    if (!DataUtils.isLogin(mContext)) {
                        ((BaseActivity) mContext).onLogoutStatus();
                    }
                    break;
                case R.id.iv_guide:
                    RouteUtil.toRechargeActivity((BaseActivity) mContext);
                    break;
            }
        }

        @Override
        public void displayImage(String imageURL, ImageView imageView) {
            GlideUtils.showImageOptions(mContext, imageURL, imageView);
        }

        @Override
        public void onImageClick(ImageItemBean info, int postion, View imageView) {
            mHomeListener.onClickBanner(info.url);
        }
    }

    private class BannerViewHolder extends RecyclerView.ViewHolder implements ImageCycleView.ImageCycleViewListener {

        private ImageCycleView icv_image;

        public BannerViewHolder(View itemView) {
            super(itemView);
            icv_image = itemView.findViewById(R.id.icv_image);
        }

        private void setData() {
            LogUtil.d("banner setdata");
            if (mImageList.size() > 0) {
                icv_image.setImageResources(mImageList, this);
                icv_image.setVisibility(View.VISIBLE);
            } else {
                icv_image.setVisibility(View.GONE);
            }
        }

        @Override
        public void displayImage(String imageURL, ImageView imageView) {
            GlideUtils.showImageRoundOptions(mContext, imageURL, imageView, 6);
        }

        @Override
        public void onImageClick(ImageItemBean info, int position, View imageView) {
            mHomeListener.onClickBanner(info.url);
        }
    }

    private class MenuViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private View rl_ticket;
        private View rl_study;
        private View rl_follow;
        private View rl_customer;


        MenuViewHolder(View itemView) {
            super(itemView);
            rl_study = itemView.findViewById(R.id.rl_study);
            rl_customer = itemView.findViewById(R.id.rl_customer);
            rl_ticket = itemView.findViewById(R.id.rl_ticket);
            rl_follow = itemView.findViewById(R.id.rl_follow);


            rl_study.setOnClickListener(this);
            rl_customer.setOnClickListener(this);
            rl_ticket.setOnClickListener(this);
            rl_follow.setOnClickListener(this);
        }

        private void setData() {
            LogUtil.d("menu setdata");
        }

        @Override
        public void onClick(View v) {
            if (FastClickUtil.isFastClick()) {
                return;
            }
            switch (v.getId()) {
                case R.id.rl_study:
                    mHomeListener.onClickInvestSchool();
                    break;
                case R.id.rl_ticket:
                    mHomeListener.onClickTicket();
                    break;
                case R.id.rl_customer:
                    if (((BaseActivity) mContext).isUserValidLogin()) {
                        RouteUtil.toWelfareActivity((BaseActivity) mContext);
                    }
                    break;
                case R.id.rl_follow:
                    mHomeListener.onClickFollowOrder();
                    break;

            }
        }
    }

    private class HotProViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private final RecyclerView rv_list;
        //        private final ImageView iv_show_all;
        private HomeHotItemAdapter homeHotItemAdapter;

        HotProViewHolder(View itemView) {
            super(itemView);
            rv_list = itemView.findViewById(R.id.rv_list);
//            iv_show_all = itemView.findViewById(R.id.iv_show_all);
            LinearLayoutManager manager = new LinearLayoutManager(mContext) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            manager.setSmoothScrollbarEnabled(false);
            rv_list.setHasFixedSize(true);
            rv_list.setNestedScrollingEnabled(false);
            rv_list.setFocusable(false);
            rv_list.setLayoutManager(manager);
//            iv_show_all.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (homeHotItemAdapter != null) {
////                        homeHotItemAdapter.setOpenAll(true);
//                        iv_show_all.setVisibility(View.GONE);
//                    }
//                }
//            });
        }

        private void setData() {
            if (homeHotItemAdapter == null) {
                homeHotItemAdapter = new HomeHotItemAdapter();
                rv_list.setAdapter(homeHotItemAdapter);
            }
            homeHotItemAdapter.setDataList(mGoodsList);
            homeHotItemAdapter.setOnItemClickListener(new OnItemClickListener<GoodsAllBean>() {
                @Override
                public void onItemClick(List<GoodsAllBean> data, View view, int position, long id) {
                    if (null != mGoodsList && mGoodsList.size() > 0) {
                        if (position != mGoodsList.size()) {
                            GoodsAllBean mGroupBean = mGoodsList.get(position);
                            mHomeListener.onClickHotPro(mGroupBean);
                        }
                    } else {
                        ToastUtil.showToast(mContext, "加载商品失败，请重启应用");
                    }
                }
            });
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
//                case R.id.iv_home_open:
//                    isOpen = !isOpen;
//                    if (isOpen) {
//                        iv_home_open.setImageResource(R.mipmap.icon_home_arrow_close);
//                    } else {
//                        iv_home_open.setImageResource(R.mipmap.icon_home_arrow_open);
//                    }
//                    adapter.setOpenAll(isOpen);
//                    break;
            }
        }
    }

    private class AdvertViewHolder extends RecyclerView.ViewHolder {

        private final ImageView iv_gif;

        public AdvertViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_gif = itemView.findViewById(R.id.iv_gif);
            iv_gif.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RouteUtil.routePageByUrl(mActivity, mGifMap.get("1").link);
                }
            });
        }

        private void setData() {
            GifBean bean = mGifMap.get("1");
            if (bean == null || TextUtils.isEmpty(bean.img)) {
                iv_gif.setVisibility(View.GONE);
            } else {
                iv_gif.setVisibility(View.VISIBLE);
                GlideUtils.showImageFitxyOptions(mContext, bean.img, iv_gif);
            }
        }
    }

    private class ImportantDataViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private final RecyclerView rv_important;
        private final ImportantAdapter importantAdapter;
        private final LinearLayout rl_border, ll_more;

        ImportantDataViewHolder(View itemView) {
            super(itemView);
            rv_important = itemView.findViewById(R.id.rv_important);
            rl_border = itemView.findViewById(R.id.rl_border);
            ll_more = itemView.findViewById(R.id.ll_more);
            importantAdapter = new ImportantAdapter(mContext, null);
            importantAdapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(List data, View view, int position, long id) {
                    HomeHotBean hotData = (HomeHotBean) data.get(position);
                    RouteUtil.toHotNewsDetailActivity((BaseActivity) mContext, hotData.title, hotData.content);
                }
            });
            rv_important.setLayoutManager(new LinearLayoutManager(mContext));
            rv_important.setAdapter(importantAdapter);
            ll_more.setOnClickListener(this);
        }

        private void setData() {
            if (mHomeHotList.isEmpty()) {
                rl_border.setVisibility(View.GONE);
            } else {
                rl_border.setVisibility(View.VISIBLE);
                importantAdapter.setData(mHomeHotList);
            }
        }

        @Override
        public void onClick(View view) {
            if (FastClickUtil.isFastClick()) {
                return;
            }
            switch (view.getId()) {
                case R.id.rl_border:
                    mHomeListener.onClickImportantData(impDataBean.link, impDataBean.title);
                    break;
                case R.id.ll_more:
                    RouteUtil.gotoActivity(mContext, HotTopicActivity.class, null);
                    break;
            }
        }
    }

    private class ProfitSquareViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private LinearLayout ll_profit;
        private TextViewSwitcher ts_notice;


        public ProfitSquareViewHolder(View itemView) {
            super(itemView);
            ll_profit = itemView.findViewById(R.id.ll_profit);
            ts_notice = itemView.findViewById(R.id.ts_notice);

            ll_profit.setOnClickListener(this);
        }

        public void setData() {
            ll_profit.setVisibility(View.GONE);

//            if (null != profitSquareList && profitSquareList.size() > 0) {
//                ll_profit.setVisibility(View.VISIBLE);
//
//                ts_notice.setAdapter(new RollingTextAdapter() {
//                    @Override
//                    public int getCount() {
//                        return profitSquareList.size() > 0 ? profitSquareList.size() : 0;
//                    }
//
//                    @Override
//                    public View getView(Context context, View contentView, int position) {
//                        View view = View.inflate(context, R.layout.item_home_notice_scroll, null);
//                        TextView tv_describe = view.findViewById(R.id.tv_describe);
//                        CustomTextView tv_rate_number = view.findViewById(R.id.tv_rate_number);
//                        CustomTextView tv_win_money = view.findViewById(R.id.tv_win_money);
//
//                        SellOrderInfoBean bean = profitSquareList.get(position);
//                        String describe = bean.nick_name;
//                        if (bean.pro_name != null && bean.pro_name.length() > 1) {
//                            describe += "「" + bean.pro_name.substring(0, 1) + "商品」 用时" + FormatUtil.dateToStamp(bean.build_time_unix, bean.liqui_time_unix);
//                        }
//                        double orderMoney = ArithUtil.mul(bean.unit_price, bean.amount);
//                        double present = 0;
//                        if (orderMoney != 0) {
//                            present = ArithUtil.mul(ArithUtil.div(Double.valueOf(bean.pro_loss), orderMoney), 100);
//                        }
//                        tv_rate_number.setText(ArithUtil.format(present) + "%");
//                        tv_win_money.setText(ArithUtil.format(Double.valueOf(bean.pro_loss)) + "元");
//                        tv_describe.setText(describe);
//                        return view;
//                    }
//                });
//
//                if (profitSquareList.size() > 1) {
//                    ts_notice.startFlipping();
//                }
//            } else {
//                ll_profit.setVisibility(View.GONE);
//            }

        }

        @Override
        public void onClick(View v) {
            if (FastClickUtil.isFastClick()) {
                return;
            }
            switch (v.getId()) {
                case R.id.ll_profit:
                    mHomeListener.onClickProfitSquare();
                    break;
            }
        }
    }

    private class SafeTipViewHolder extends RecyclerView.ViewHolder {

        private final ImageView iv_tip;

        public SafeTipViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_tip = itemView.findViewById(R.id.iv_tip);
        }

        public void setData() {
            if (isShowTip) {
//                iv_tip.setVisibility(View.VISIBLE);
            } else {
                iv_tip.setVisibility(View.GONE);
            }
        }
    }

    private class MarketAndOrderViewHolder extends RecyclerView.ViewHolder implements RadioGroup.OnCheckedChangeListener {

        private final RadioGroup rg_group;
        private final RecyclerView rv_list;
        private LinearLayout ll_no_data;
        private HomeGoodsAdapter homeGoodsAdapter;
        //        private FollowOrderAdapter followOrderAdapter;
        private CattleBillAdapter cattleAdapter;
        private EconomicsAdapter mEconomicsAdapter;
        private GridLayoutManager gridLayoutManager;
        private LinearLayoutManager linearLayoutManager;
        private final MyGridView gv_day;

        // 跟踪数据是否已加载
        private boolean[] dataLoaded = new boolean[3]; // [商品, 跟单, 日历]
        private HomeDayAdapter dayAdapter;

        public MarketAndOrderViewHolder(@NonNull View itemView) {
            super(itemView);
            rg_group = itemView.findViewById(R.id.rg_group);
            ll_no_data = itemView.findViewById(R.id.ll_no_data);
            rv_list = itemView.findViewById(R.id.rv_list);
            gv_day = itemView.findViewById(R.id.gv_day);

            // 预先创建布局管理器
            gridLayoutManager = new GridLayoutManager(mContext, 2) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };

            linearLayoutManager = new LinearLayoutManager(mContext) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            rv_list.setHasFixedSize(true);
            rv_list.setNestedScrollingEnabled(false);
            rv_list.setFocusable(false);

            initAdapters();

            rg_group.setOnCheckedChangeListener(this);

            // 设置默认选项
            setTextStyle(mChooseProfit);
        }

        private void initAdapters() {
            // 初始化商品列表适配器
            homeGoodsAdapter = new HomeGoodsAdapter();
            homeGoodsAdapter.setOnItemClickListener(new OnItemClickListener<ShopGoodsListBean>() {
                @Override
                public void onItemClick(List<ShopGoodsListBean> data, View view, int position, long id) {
                    mHomeListener.onClickBuyGoods(data.get(position));
                }
            });

            // 初始化跟单适配器
            cattleAdapter = new CattleBillAdapter(mContext, profitSquareList);
            cattleAdapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(List data, View view, int position, long id) {
                    SellOrderInfoBean bean = (SellOrderInfoBean) profitSquareList.get(position);
                    RouteUtil.toMarketActivity((BaseActivity) mContext, bean.pro_code);
                }
            });
//            followOrderAdapter.setFollowOrderListener(new FollowOrderAdapter.FollowOrderListener() {
//                @Override
//                public void onClickDetail(String userId) {
//                    RouteUtil.toFollowOrderUserActivity(mActivity, userId);
//                }
//
//                @Override
//                public void onClickFollow(int position) {
//                    if (mFollowList == null || mFollowList.isEmpty()) return;
//                    if (!DataUtils.getUserId(mActivity).equals("0") && DataUtils.isLogin(mActivity)) {
//                        //更新用户信息
//                        myApplication.updateUserDetail();
//                        FollowOrderBean bean = mFollowList.get(position);
//                        GoodsAllBean mGroupBean = myApplication.getAllGoodMap().get(bean.pro_code);
//                        mHomeListener.onClickFollowGroupBean(mGroupBean);
//                        if (null != mGroupBean) {
//                            CreateOrderModule.getInstance().createOrder(mActivity, bean.trade_type, bean.order_id, mGroupBean, bean.unit_price, bean.user_id, 0);
//                        } else {
//                            ToastUtil.showToast(mActivity, "未获取的产品信息");
//                        }
//                    } else {
//                        RouteUtil.toLoginRegisterActivity(mActivity, LoginRegisterActivity.TYPE_LOGIN);
//                    }
//                }
//
//                @Override
//                public void onClickGoKLine(int position) {
//                    if (mFollowList == null || mFollowList.isEmpty()) return;
//                    FollowOrderBean bean = mFollowList.get(position);
//                    GoodsAllBean mGroupBean = myApplication.getAllGoodMap().get(bean.pro_code);
//                    RouteUtil.toMarketActivity(mActivity, mGroupBean.pro_code);
//                }
//            });
            // 初始化财经日历适配器
            mEconomicsAdapter = new EconomicsAdapter(mContext, mCalendarList);
            mEconomicsAdapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(List data, View view, int position, long id) {
                    mHomeListener.onClickCalendar(mCalendarList.get(position));
                }
            });
            dayAdapter = new HomeDayAdapter(mContext, mDayList);
            gv_day.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    dayAdapter.setPosition(position);
                    mHomeListener.onClickDay(mDayList.get(position));
                }
            });
        }


        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int oldType = mChooseProfit;
            switch (checkedId) {
                case R.id.rb_follow_order:
                    mChooseProfit = 0;
                    break;
                case R.id.rb_calendar:
                    mChooseProfit = 1;
                    break;
                case R.id.rb_goods_mail:
                    mChooseProfit = 2;
                    break;

            }

            if (oldType != mChooseProfit) {
                setTextStyle(mChooseProfit);
//                mHomeListener.onClickListType(mChooseProfit);
                showListByType(mChooseProfit);
            }
        }

        private void showListByType(int type) {
            // 检查数据是否为空
            boolean isEmpty = true;
            switch (type) {
                case 0: // 跟单
                    isEmpty = profitSquareList.isEmpty();
                    if (!isEmpty) {
                        rv_list.setLayoutManager(linearLayoutManager);
                        rv_list.setAdapter(cattleAdapter);
                        // 只有第一次加载或有新数据时更新适配器
                        if (!dataLoaded[0] || cattleAdapter.getItemCount() != profitSquareList.size()) {
                            cattleAdapter.setData(profitSquareList);
                            dataLoaded[0] = true;
                        }
                    }
                    break;

                case 1: // 日历
                    isEmpty = mCalendarList.isEmpty();
                    gv_day.setAdapter(dayAdapter);
                    if (!isEmpty) {
                        rv_list.setLayoutManager(linearLayoutManager);
                        rv_list.setAdapter(mEconomicsAdapter);
                        if (!dataLoaded[1] || mEconomicsAdapter.getItemCount() != mCalendarList.size()) {
                            // 更新数据
                            mEconomicsAdapter.setData(mCalendarList);
                        }
                        rv_list.setAdapter(mEconomicsAdapter);
                        dataLoaded[1] = true;
                    }
                    break;

                case 2: // 商品
                    isEmpty = mHomeGoodsList.isEmpty();
                    if (!isEmpty) {
                        rv_list.setLayoutManager(gridLayoutManager);
                        rv_list.setAdapter(homeGoodsAdapter);
                        // 只有第一次加载或有新数据时更新适配器
                        if (!dataLoaded[2] || homeGoodsAdapter.getItemCount() != mHomeGoodsList.size()) {
                            homeGoodsAdapter.setShopGoodsListBeans(mHomeGoodsList);
                            dataLoaded[2] = true;
                        }
                    }
                    break;
            }
            // 根据数据是否为空更新视图
            ll_no_data.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
            rv_list.setVisibility(isEmpty ? View.GONE : View.VISIBLE);
            gv_day.setVisibility(type == 1 ? View.VISIBLE : View.GONE);
        }

        private void setTextStyle(int index) {
            for (int i = 0; i < rg_group.getChildCount(); i++) {
                RadioButton childAt = (RadioButton) rg_group.getChildAt(i);
                if (index == i) {
                    childAt.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                } else {
                    childAt.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                }
            }
        }

        public void setData() {
            showListByType(mChooseProfit);
        }
    }

    public interface HomeListener {
        /**
         * 点击Banner
         *
         * @param url
         */
        void onClickBanner(String url);

        /**
         * 点击热门产品
         */
        void onClickHotPro(GoodsAllBean bean);

        /**
         * 点击新手秘籍
         */
        void onClickInvestSchool();

        /**
         * 点击跟单
         */
        void onClickFollowOrder();

        /**
         * 点击牛单广场
         */
        void onClickProfitSquare();

        /**
         * 点击客服
         */
        void onClickCustomService();

        /**
         * 点击重要数据详情
         */
        void onClickImportantData(String url, String title);

        /**
         * 新手送券
         */
        void onClickTicket();

        /**
         * 消息列表
         */
        void onClickMessage();

        void onClickBuyGoods(ShopGoodsListBean bean);

        /**
         * 跟单弹窗回调，刷新最新价格
         *
         * @param bean
         */
        void onClickFollowGroupBean(GoodsAllBean bean);

        /**
         * 点击财经日历
         *
         * @param bean
         */
        void onClickCalendar(CalendarBean bean);

        /**
         * 点击日期
         *
         * @param bean
         */
        void onClickDay(DayBean bean);

    }
}
